package su.user.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.kuakao.core.dto.PagerDTO;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import su.base.Constants;
import su.base.dao.SuBaseDAO;
import su.user.dao.IUserCustomerDAO;
import su.user.entity.UserCustomer;


@Repository
public class UserCustomerDAO extends SuBaseDAO implements IUserCustomerDAO {

	public List<Map> queryByPage(PagerDTO s)throws SQLException{
		String sql = "select * from su_user_customer where status=1";
		if(StringUtils.isNotBlank(s.getS1())){//id
			if(s.getS1().indexOf(",")>-1){
				sql += " and id in ("+s.getS1()+")";
			}else{
				sql += " and id="+s.getS1();
			}
		}
		if(StringUtils.isNotBlank(s.getS2())){//简拼
			sql += " and jianpin like '%"+s.getS2()+"%'";
		}
		if(StringUtils.isNotBlank(s.getS3())){//省份
			sql += " and provientid="+s.getS3();
		}
		if(StringUtils.isNotBlank(s.getS4())){//业务员
			sql += " and ywyid="+s.getS4();
		}
		
		sql += " ORDER BY id desc";
		
		return super.queryByPage(sql, new RowMapper<Map>() {

			public Map mapRow(ResultSet rs, int arg1) throws SQLException {
				Map map = new HashMap();
				map.put("id", rs.getInt("id"));
				map.put("customername", rs.getString("customername"));
				map.put("jianpin", rs.getString("jianpin"));
				map.put("ywyid", rs.getInt("ywyid"));
				map.put("provientid", rs.getInt("provientid"));
				map.put("postcode", rs.getString("postcode"));
				map.put("address", rs.getString("address"));
				map.put("tel", rs.getString("tel"));
				map.put("fax", rs.getString("fax"));
				map.put("taxnum", rs.getString("taxnum"));
				map.put("bank", rs.getString("bank"));
				map.put("banknum", rs.getString("banknum"));
				map.put("shipmenttype", rs.getString("shipmenttype"));
				map.put("shipmentaddr", rs.getString("shipmentaddr"));
				map.put("invoice", rs.getString("invoice"));
				map.put("customerdesc", rs.getString("customerdesc"));
				map.put("status", rs.getInt("status"));
				return map;
			}
			
		}, s);
	}
	
	public UserCustomer getById(int id)throws SQLException{
		String sql = "select * from su_user_customer where status=1 and id=?";
		return super.queryForObject(sql,new Object[]{id},new RowMapper<UserCustomer>(){
			public UserCustomer mapRow(ResultSet rs, int arg1) throws SQLException {
				UserCustomer entity = new UserCustomer();
				entity.setId(rs.getInt("id"));
				entity.setCustomername(rs.getString("customername"));
				entity.setJianpin(rs.getString("jianpin"));
				entity.setYwyid(rs.getInt("ywyid"));
				entity.setProvientid(rs.getInt("provientid"));
				entity.setPostcode(rs.getString("postcode"));
				entity.setAddress(rs.getString("address"));
				entity.setTel(rs.getString("tel"));
				entity.setFax(rs.getString("fax"));
				entity.setTaxnum(rs.getString("taxnum"));
				entity.setBank(rs.getString("bank"));
				entity.setBanknum(rs.getString("banknum"));
				entity.setShipmenttype(rs.getString("shipmenttype"));
				entity.setShipmentaddr(rs.getString("shipmentaddr"));
				entity.setInvoice(rs.getString("invoice"));
				entity.setCustomerdesc(rs.getString("customerdesc"));
				entity.setStatus(rs.getInt("status"));
				return entity;
			}
			
		});
	}
	
	public void insert(UserCustomer o)throws SQLException{
		String sql = "insert into su_user_customer(customername,jianpin,ywyid,provientid,postcode,address,tel,fax,taxnum,bank,banknum,shipmenttype,shipmentaddr,invoice,customerdesc,status) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		super.update(sql, new Object[]{o.getCustomername(),o.getJianpin(),o.getYwyid(),o.getProvientid(),
				o.getPostcode(),o.getAddress(),o.getTel(),o.getFax(),
				o.getTaxnum(),o.getBank(),o.getBanknum(),o.getShipmenttype(),
				o.getShipmentaddr(),o.getInvoice(),o.getCustomerdesc(),o.getStatus()});
	}
	
	public void update(UserCustomer o)throws SQLException{
		String sql = "update su_user_customer set customername=?,jianpin=?,ywyid=?,provientid=?,postcode=?,address=?,tel=?,fax=?,taxnum=?,bank=?,banknum=?,shipmenttype=?,shipmentaddr=?,invoice=?,customerdesc=?,status=? where id=?";
		super.update(sql, new Object[]{o.getCustomername(),o.getJianpin(),o.getYwyid(),o.getProvientid(),
				o.getPostcode(),o.getAddress(),o.getTel(),o.getFax(),
				o.getTaxnum(),o.getBank(),o.getBanknum(),o.getShipmenttype(),
				o.getShipmentaddr(),o.getInvoice(),o.getCustomerdesc(),o.getStatus(),o.getId()});
	}

	public void remove(int id)throws SQLException{
		String sql = "update su_user_customer set status=? where id=?";
		super.update(sql, new Object[]{Constants.STATUS_DISABLE,id});
	}
	
}

